VERSION 0.6

FROM alpine:3.15

buildkitd:
    ARG BUILDKIT_PROJECT
    IF [ "$BUILDKIT_PROJECT" != "" ]
        IF case "$BUILDKIT_PROJECT" in "../*") true;; "*") false;; esac
            # Assuming this is coming from the main Earthly Earthfile.
            ARG BUILDKIT_BASE_IMAGE=../$BUILDKIT_PROJECT+build
        ELSE
            ARG BUILDKIT_BASE_IMAGE=$BUILDKIT_PROJECT+build
        END
    ELSE
        ARG BUILDKIT_BASE_IMAGE=github.com/earthly/buildkit:f126d5cedf8a91de9948f374b982cce3fb5649f6+build
    END
    FROM $BUILDKIT_BASE_IMAGE
    RUN echo "@edge-community http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
    RUN apk add --update --no-cache \
        cni-plugins@edge-community \
        gettext \
        iptables \
        openssh-client \
        pigz \
        util-linux \
        xz

    # Add github and gitlab to known hosts.
    RUN mkdir -p ~/.ssh
    RUN echo "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==" >> ~/.ssh/known_hosts
    RUN echo "gitlab.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCsj2bNKTBSpIYDEGk9KxsGh3mySTRgMtXL583qmBpzeQ+jqCMRgBqB98u3z++J1sKlXHWfM9dyhSevkMwSbhoR8XIq/U0tCNyokEi/ueaBMCvbcTHhO7FcwzY92WK4Yt0aGROY5qX2UKSeOvuP4D6TPqKF1onrSzH9bx9XUf2lEdWT/ia1NEKjunUqu1xOB/StKDHMoX4/OKyIzuS0q/T1zOATthvasJFoPrAjkohTyaDUz2LN5JoH839hViyEG82yB+MjcFV5MU3N1l1QL3cVUCh93xSaua1N85qivl+siMkPGbO5xR/En4iEY6K2XPASUEMaieWVNTRCtJ4S8H+9" >> ~/.ssh/known_hosts

    # Add the config templates and our own wrapper script.
    COPY ./entrypoint.sh /usr/bin/entrypoint.sh
    COPY ./buildkitd.toml.template /etc/buildkitd.toml.template
    COPY ./buildkitd.cache.template /etc/buildkitd.cache.template
    COPY ./buildkitd.tcp.template /etc/buildkitd.tcp.template
    COPY ./buildkitd.tls.template /etc/buildkitd.tls.template
    COPY ./cni-conf.json.template /etc/cni/cni-conf.json.template

    # Scripts and binaries used for the builds.
    COPY ../+shellrepeater/shellrepeater /usr/bin/shellrepeater
    COPY ../+debugger/earth_debugger /usr/bin/earth_debugger
    COPY ./dockerd-wrapper.sh /var/earthly/dockerd-wrapper.sh
    COPY ./docker-auto-install.sh /var/earthly/docker-auto-install.sh

    ENV EARTHLY_RESET_TMP_DIR=false
    ENV EARTHLY_TMP_DIR=/tmp/earthly
    ENV BUILDKIT_DEBUG=false
    ENV BUILDKIT_MAX_PARALLELISM=20
    ENV CACHE_SIZE_MB=0
    ENV CACHE_SIZE_PCT=0
    ENV NETWORK_MODE=cni
    ARG EARTHLY_GIT_HASH
    ENV EARTHLY_GIT_HASH=$EARTHLY_GIT_HASH
    ENV BUILDKIT_BASE_IMAGE=$BUILDKIT_BASE_IMAGE
    ENV EARTHLY_CACHE_VERSION="2" # whenever this value changes, a forced cache reset is performed
    VOLUME /tmp/earthly
    ENTRYPOINT ["/usr/bin/entrypoint.sh", "buildkitd", "--config=/etc/buildkitd.toml"]
    ARG EARTHLY_TARGET_TAG_DOCKER
    ARG TAG="dev-$EARTHLY_TARGET_TAG_DOCKER"
    ARG REGISTRY_USER="earthly"
    ARG IMAGE_REGISTRY="docker.io"

    # Ensure that the image we save is a fully qualified name. Podman defaults to "localhost", while Docker defaults to
    # "docker.io". This keeps it consistent across both frontends.
    SAVE IMAGE --push --cache-from=$IMAGE_REGISTRY/earthly/buildkitd:main $IMAGE_REGISTRY/$REGISTRY_USER/buildkitd:$TAG

buildkit-sha:
    RUN apk add git
    ARG BUILDKIT_GIT_SHA
    ARG BUILDKIT_GIT_BRANCH=earthly-main
    RUN --no-cache set -e; \
        if [ "$(echo -n $BUILDKIT_GIT_SHA | wc -c)" = 40 ]; then \
            echo "pinning github.com/earthly/buildkit to reference git sha1: $BUILDKIT_GIT_SHA"; \
            buildkit_sha1="$BUILDKIT_GIT_SHA"; \
        else \
            test -z "$BUILDKIT_GIT_SHA"; \
            echo "looking up branch $BUILDKIT_GIT_BRANCH"; \
            buildkit_sha1=$(git ls-remote --refs -q https://github.com/earthly/buildkit.git "$BUILDKIT_GIT_BRANCH" | awk 'BEGIN { FS = "[ \t]+" } {print $1}'); \
            echo "pinning github.com/earthly/buildkit@${BUILDKIT_BRANCH} to reference git sha1: $buildkit_sha1"; \
        fi && \
        test -n "$buildkit_sha1" && \
        echo "$buildkit_sha1" > buildkit_sha
    SAVE ARTIFACT buildkit_sha buildkit_sha

update-buildkit:
    LOCALLY
    ARG --required BUILDKIT_GIT_SHA
    RUN sed -i 's/\(^[ \t]\+ARG BUILDKIT_BASE_IMAGE=github.com\/earthly\/buildkit\).*/\1:'$BUILDKIT_GIT_SHA'+build/g' Earthfile
